home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 8434 / 8434.xpi / chrome / content / editor.js < prev    next >
Text File  |  2008-11-16  |  5KB  |  110 lines

  1. var colorChosen = false;
  2. var rainbowPickerJustChanged = false;
  3. const nsIFilePicker = Components.interfaces.nsIFilePicker;
  4. function insertCodeAtStart(snippet) {
  5.     var position = codeElement.value.indexOf(snippet);
  6.     if (position == -1) {
  7.         if (codeElement.value.length > 0) {
  8.             codeElement.value = snippet + "\n" + codeElement.value;
  9.         } else {
  10.             codeElement.value = snippet + "\n";
  11.         }
  12.     }
  13.     codeElement.setSelectionRange(snippet.length + 1, snippet.length + 1);
  14.     codeElement.focus();
  15. }
  16. function insertCodeAtCaret(snippet) {
  17.     var selectionEnd = codeElement.selectionStart + snippet.length;
  18.     codeElement.value = codeElement.value.substring(0, codeElement.selectionStart) + snippet + codeElement.value.substring(codeElement.selectionEnd, codeElement.value.length);
  19.     codeElement.focus();
  20.     codeElement.setSelectionRange(selectionEnd, selectionEnd);
  21. }
  22. function insertChromePath() {
  23.     var ios = Components.classes['@mozilla.org/network/io-service;1'].getService(Components.interfaces.nsIIOService);
  24.     var fileHandler = ios.getProtocolHandler("file").QueryInterface(Components.interfaces.nsIFileProtocolHandler);
  25.     var chromePath = fileHandler.getURLSpecFromFile(Components.classes['@mozilla.org/file/directory_service;1'].getService(Components.interfaces.nsIProperties).get("UChrm", Components.interfaces.nsIFile));
  26.     insertCodeAtCaret(chromePath);
  27. }
  28. function chooseColor(event) {
  29.     colorChosen = true;
  30.     var parent = event.target.parentNode;
  31.     while (parent != null) {
  32.         switch (parent.nodeName) {
  33.           case "menupopup":
  34.             parent.hidePopup();
  35.             break;
  36.           case "button":
  37.             parent.open = false;
  38.           default:;
  39.         }
  40.         parent = parent.parentNode;
  41.     }
  42.     setTimeout(insertColor, 1);
  43. }
  44. function insertColor() {
  45.     if (colorChosen) {
  46.         insertCodeAtCaret(document.getElementById("normal-colorpicker").color);
  47.         colorChosen = false;
  48.     }
  49. }
  50. function insertRainbowPickerColor(event) {
  51.     if (rainbowPickerJustChanged) {
  52.         return;
  53.     }
  54.     rainbowPickerJustChanged = true;
  55.     setTimeout(function () {rainbowPickerJustChanged = false;}, 100);
  56.     insertCodeAtCaret(event.target.color);
  57. }
  58. function openSitesDialog() {
  59.     openDialog("chrome://googleredesigned/content/specifySites.xul", "googleredesignedSpecifySites", "chrome,modal,resizable,centerscreen", this.applySpecifySite);
  60. }
  61. function changeWordWrap(on) {
  62.     var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefService);
  63.     prefs = prefs.getBranch("extensions.googleredesigned.");
  64.     prefs.setBoolPref("wrap_lines", on);
  65.     refreshWordWrap(on);
  66. }
  67. function refreshWordWrap(on) {
  68.     codeElement.style.display = "none";
  69.     codeElement.setAttribute("wrap", on ? "on" : "off");
  70.     setTimeout("document.getElementById('code').style.display = '';", 10);
  71. }
  72. function insertDataURI() {
  73.     var fp = Components.classes['@mozilla.org/filepicker;1'].createInstance(nsIFilePicker);
  74.     fp.init(window, STRINGS.getString("dataURIDialogTitle"), nsIFilePicker.modeOpen);
  75.     if (fp.show() != nsIFilePicker.returnOK) {
  76.         return;
  77.     }
  78.     var file = fp.file;
  79.     var contentType = Components.classes['@mozilla.org/mime;1'].getService(Components.interfaces.nsIMIMEService).getTypeFromFile(file);
  80.     var inputStream = Components.classes['@mozilla.org/network/file-input-stream;1'].createInstance(Components.interfaces.nsIFileInputStream);
  81.     inputStream.init(file, 1, 384, 0);
  82.     var stream = Components.classes['@mozilla.org/binaryinputstream;1'].createInstance(Components.interfaces.nsIBinaryInputStream);
  83.     stream.setInputStream(inputStream);
  84.     var encoded = btoa(stream.readBytes(stream.available()));
  85.     stream.close();
  86.     inputStream.close();
  87.     insertCodeAtCaret("data:" + contentType + ";base64," + encoded);
  88. }
  89. function makeImportant() {
  90.     var box = codeElement.mInputField;
  91.     var scroll = [box.scrollTop, box.scrollLeft];
  92.     var code = codeElement.value;
  93.     code = code.replace(/;base64/g, "__base64__");
  94.     var declarationBlocks = code.match(/\{[^\{\}]*[\}]/g);
  95.     if (declarationBlocks == null) {
  96.         return;
  97.     }
  98.     var declarations = [];
  99.     declarationBlocks.forEach(function (declarationBlock) {declarations = declarations.concat(declarationBlock.split(/;/));});
  100.     declarations = declarations.filter(function (declaration) {return /[A-Za-z0-9-]+\s*:\s*[^};]+/.test(declaration) && !/!\s*important/.test(declaration);});
  101.     declarations = declarations.map(function (declaration) {return declaration.match(/[A-Za-z0-9-]+\s*:\s*[^};]+/)[0].replace(/\s+$/, "");});
  102.     var replacements = [];
  103.     declarations.forEach(function (declaration) {var replacement = {hash: Math.random(), value: declaration};replacements.push(replacement);code = code.replace(replacement.value, replacement.hash);});
  104.     replacements.forEach(function (replacement) {code = code.replace(replacement.hash, replacement.value + " !important");});
  105.     code = code.replace(/__base64__/g, ";base64");
  106.     codeElement.value = code;
  107.     box.scrollTop = scroll[0];
  108.     box.scrollLeft = scroll[1];
  109. }
  110.